# -*- coding: utf-8 -*-
"""
-------------------------------------------
    Description:
    Author:             yu_lei
    Email:              lyu@topxgun.com
    Date:               2022/03/31
-------------------------------------------
"""
import logging
import sys

from data.gcs_logger import logger_init

logger_init("CTRL_PAD_")  # 该函数的调用要在所有其他logger函数调用之前进行
from PyQt5.QtWidgets import QStatusBar
from PyQt5 import QtWidgets, QtGui
from gcs_views.ui_control_pad import Ui_ControlPad
from data.DataHandler import datahandler
from data.defines import *


class ControlPad(Ui_ControlPad):
    remote_upload_on = 1

    def __init__(self):
        super(ControlPad, self).__init__()

    def setupControlPad(self):
        self.statusBar = QStatusBar()
        datahandler.record_on = False  # 关闭数据记录功能
        datahandler.upload_switch(1)  # 打开上行数据发送功能
        # 初始化控制面板

        self.btn_1.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RESET))
        self.btn_2.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_TAXI))
        self.btn_3.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_BRAKING))
        self.btn_4.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ANGLE_OF_TAKEOFF))
        self.btn_5.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_TAKE_OFF))
        self.btn_6.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_NAVI))
        self.btn_7.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_CLIMB_UP))
        self.btn_8.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LEVEL_FLIGHT))
        self.btn_9.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_SLIDE))
        self.btn_10.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LAND1))
        self.btn_11.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LAND2))
        self.btn_12.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_TURN_LEFT))
        self.btn_13.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_DIRECT_FLIGHT))
        self.btn_14.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_TURN_RIGHT))
        self.btn_15.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_DIRECTION))
        self.btn_16.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_CIRCLE))
        self.btn_17.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_GO_AROUND))
        self.btn_18.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_CUT_IN_LOW))
        self.btn_19.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_CUT_OUT_LOW))
        self.btn_20.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_TAKE_OFF))
        self.btn_21.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_IDLING))
        self.btn_22.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_CLIMB))
        self.btn_23.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_CRUISE_BIG))
        self.btn_24.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_CRUISE_HALF))
        self.btn_25.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_CRUISE_SMALL))
        self.btn_26.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_SLIDE_BIG))
        self.btn_27.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_THR_SLIDE_SMALL))
        self.btn_28.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ACCESS_SPEED_CON))
        self.btn_29.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_DISCONNECT_SPEED_CON))
        self.btn_30.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_PRE_STOP))
        self.btn_31.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_STOP))
        self.btn_32.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LOSS_PRO_ON))
        self.btn_33.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LOSS_PRO_OFF))
        self.btn_34.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ROUTE_ON))
        self.btn_35.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ROUTE_OFF))
        self.btn_36.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ROUTE_PRE_DEL))
        self.btn_37.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ROUTE_DEL))
        self.btn_38.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RUDDER_USE))
        self.btn_39.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RUDDER_DISABLE))
        self.btn_40.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_A_CLOSE))
        self.btn_41.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_B_CLOSE))
        self.btn_42.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALL_DISCONNECT))
        self.btn_43.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALL_CLOSE))
        self.btn_44.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_PRESS_RUDDER_OPEN))
        self.btn_45.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_PRESS_RUDDER_CLOSE))
        self.btn_46.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_MASTER_POS))
        self.btn_47.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_GPS_POS))
        self.btn_48.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_SEC_POS))
        self.btn_49.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_MASTER_ANGLE))
        self.btn_50.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_SEC_ANGLE))
        self.btn_51.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_OIL_PUMP_CLOSE))
        self.btn_52.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_OIL_PUMP_OPEN))
        self.btn_53.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_BRAKE_MAG_CLOASE))
        self.btn_54.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_BRAKE_MAG_OPEN))
        self.btn_55.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_OUTPUT_B_CLOSE))
        self.btn_56.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_OUTPUT_B_OPEN))
        self.btn_57.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_FLAP_MOTOR_CLOSE))
        self.btn_58.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_FLAP_MOTOR_0))
        self.btn_59.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_FLAP_MOTOR_15))
        self.btn_60.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_FLAP_MOTOR_30))
        self.btn_61.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_FLAP_MOTOR_45))
        self.btn_62.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LEFT_OIL))
        self.btn_63.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RIGHT_OIL))
        self.btn_64.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_MASTER_ALT))
        self.btn_65.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_GPS_ALT))
        self.btn_66.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_LINK_AIR_PRESSURE_ALT))
        self.btn_67.clicked.connect(
            lambda: datahandler.direct_send_request(0, COMMAND_CTRL_MANUAL_ACCESS_RECITY_DEVIATION))
        self.btn_68.clicked.connect(
            lambda: datahandler.direct_send_request(0, COMMAND_CTRL_MANUAL_BREAK_RECITY_DEVIATION))
        self.btn_69.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_BREAK_ROLL_VEL))
        self.btn_70.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ACCESS_ROLL_VEL))
        self.btn_71.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_BREAK_PITCH_VEL))
        self.btn_72.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ACCESS_PITCH_VEL))
        self.btn_73.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_BREAK_YAW_VEL))
        self.btn_74.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ACCESS_YAW_VEL))
        self.btn_75.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_NONE_OIL))
        self.btn_76.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_GROUND_TEST))
        self.btn_77.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_SPEED_INT_CLOSE))
        self.btn_78.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_SPEED_INT_OPEN))
        self.btn_79.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RADAR_ENABLED))
        self.btn_80.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RADAR_DISABLED))
        self.btn_81.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_VI_FILTER_ENABLED))
        self.btn_82.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_VI_FILTER_DISABLED))
        self.btn_83.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALTER1))
        self.btn_84.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALTER2))
        self.btn_85.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALTER3))
        self.btn_86.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALTER4))
        self.btn_87.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALTER5))
        self.btn_88.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_ALTER6))

        datahandler.sig_request_echo.connect(self.request_echo_process)

        self.btn_reset.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_RESET))
        self.btn_prenav.clicked.connect(lambda: datahandler.direct_send_request(0, COMMAND_CTRL_NAVI))

        self.btn_set_1.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ROUTE_NUM, [1, self.edit_1.value()]))
        self.btn_set_2.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ROUTE_DIRECT, self.edit_2.value()))
        self.btn_set_3.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_PITCH, self.edit_3.value()))
        self.btn_set_4.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ROLL, self.edit_4.value()))
        self.btn_set_5.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_YAW, self.edit_5.value()))
        self.btn_set_6.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_THR, self.edit_6.value()))
        self.btn_set_7.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_FIELD_HEIGHT, self.edit_7.value()))
        self.btn_set_8.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_BACK_WHEEL_DIFF, self.edit_8.value()))
        self.btn_set_9.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_BACK_WHEEL_BRAKE, self.edit_9.value()))
        self.btn_set_10.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ROUTE_ALT, self.edit_10.value()))
        self.btn_set_11.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_LOAD, self.edit_11.value()))
        self.btn_set_12.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_TRACE_ANGLE, self.edit_12.value()))
        self.btn_set_13.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_AIR_SPEED, self.edit_13.value()))
        self.btn_set_16.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ROUTE_ENTER, self.edit_16.value()))
        self.btn_set_17.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_LOWSPACE_FLY_ALT, self.edit_17.value()))
        self.btn_set_18.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_LIFT_RUDDER, self.edit_18.value()))
        self.btn_set_19.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_AILERON_RUDDER, self.edit_19.value()))
        self.btn_set_20.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_COURSE_RUDDER, self.edit_20.value()))
        self.btn_set_27.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_FRONT_WHEEL_DEG, self.edit_27.value()))

        self.btn_set_28.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ADC2_HEATING, self.selection_28.currentIndex()))
        self.btn_set_29.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ADC2_VIAS_RESET,
                                                    self.selection_29.currentIndex()))
        self.btn_set_30.clicked.connect(
            lambda: datahandler.direct_send_request(1, COMMAND_ADJUST_ADC2_AOA_RESET, self.selection_30.currentIndex()))

        datahandler.sig_RecvDataLink.connect(self.RecvDataLinkData)
        self.btn_send_command.clicked.connect(self.remote_switch)

    def request_echo_process(self, echo_msg):
        if isinstance(echo_msg, str):
            self.readout_last_cmd_echo.setText(echo_msg)

    def RecvDataLinkData(self, data):
        try:
            llink_connect = data["llink_connect"]
            ulink_connect = data["ulink_connect"]
            l_link_upload_freq = data["l_link_upload_freq"]
            u_link_upload_freq = data["u_link_upload_freq"]
            g_link_upload_freq = data["g_link_upload_freq"]
            download_freq = data["download_freq"]
            current_link_type = data["current_link_type"]
            tmptext = "上行链路状态：U%d L%d G%d" % (u_link_upload_freq, l_link_upload_freq, g_link_upload_freq)
            self.statusbar.showMessage(tmptext)
        except Exception as e:
            logging.error(repr(e))

    def remote_switch(self):

        # pixmapsrc = "resources/btn_command_on.png"

        if self.remote_upload_on == 0:
            self.remote_upload_on = 1
            pixmapsrc = "resources/btn_command_on.png"
            self.tabWidget.setDisabled(False)

        else:
            self.remote_upload_on = 0
            pixmapsrc = "resources/btn_command_off.png"
            self.tabWidget.setDisabled(True)

        self.btn_send_command.setIcon(QtGui.QIcon(pixmapsrc))
        datahandler.upload_switch(self.remote_upload_on)

        logging.info("设置遥控上行开关 remote_upload_on = %d", self.remote_upload_on)


# 地面站软件的入口程序
if __name__ == '__main__':
    logging.info("========== GCS Started! ==========")
    # 启动地面站主界面
    app = QtWidgets.QApplication(sys.argv)
    main_window = QtWidgets.QMainWindow()
    ui = ControlPad()
    ui.setupUi(main_window)  # 初始化所有控件和窗口布局
    ui.setupControlPad()
    main_window.show()
    sys.exit(app.exec_())
